package org.pointex.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.pointex.common.model.ParamObject;

public class MD5Util {
	// MD5 加密
	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static final String ENCODE = "UTF-8"; // GBK

	// mv6quAkxTZn0IZRqIHckPjm4nH00T1d 1 2
	public static final String MD5KEY = "09008b4ba65d0997fdf3995e8d8f1d67"; // KEY

	// MD5 带KEY加密
	public static String MD5Sign(String aValue, String aKey) {
		byte k_ipad[] = new byte[64];
		byte k_opad[] = new byte[64];
		byte keyb[];
		byte value[];
		try {
			keyb = aKey.getBytes(ENCODE);
			value = aValue.getBytes(ENCODE);
		} catch (UnsupportedEncodingException e) {
			keyb = aKey.getBytes();
			value = aValue.getBytes();
		}
		Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
		Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
		for (int i = 0; i < keyb.length; i++) {
			k_ipad[i] = (byte) (keyb[i] ^ 0x36);
			k_opad[i] = (byte) (keyb[i] ^ 0x5c);
		}

		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		md.update(k_ipad);
		md.update(value);
		byte dg[] = md.digest();
		md.reset();
		md.update(k_opad);
		md.update(dg, 0, 16);
		dg = md.digest();
		return MD5Util.toHex(dg);
	}

	public static String MD5Sign(String aValue) {
		return MD5Sign(aValue, MD5KEY);
	}

	private static String toHex(byte input[]) {
		if (input == null)
			return null;
		StringBuffer output = new StringBuffer(input.length * 2);
		for (int i = 0; i < input.length; i++) {
			int current = input[i] & 0xff;
			if (current < 16)
				output.append("0");
			output.append(Integer.toString(current, 16)); 
		}
		return output.toString();
	}
	
	
	public static String getMd5StrSign(String uid,String name){
		return MD5Util.MD5Sign(uid+name+"sign",MD5Util.MD5KEY);
	}
	
	
	
	/**
	 * 登录请求的时候验证签名
	 * uid name 
	 * 模
	 * 签名
	 * fa 为true表示登录的时候验证签名
	 * @return
	 */
	public static String getSign(ParamObject param){
		String st=null;
		if(param!=null){
			String auth="";
			Date date=new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
			String dateStr=sdf.format(date);
			auth=MD5Util.MD5(MD5Util.MD5Sign(dateStr, MD5Util.MD5KEY));
			
			auth=MD5Util.MD5Sign(param.getUid() + param.getName()+auth+"sign", MD5Util.MD5KEY);
			Random ran=new Random();
			int res=ran.nextInt(10);
			st=auth.substring(0,11)+res+auth.substring(11,auth.length()-1);
			return st;
		}
		return st;
	}
	
/*	public static void main(String[] args) {
		ParamObject param=new ParamObject();
		param.setName("123456");
		param.setUid("456789");
		String str=getSign(param);
		param.setSign(str);
		
		boolean falg=isSign(param);
	}
	*/
	
	
	
	
	/**
	 * 获取公钥的时候的 验证签名
	 * uid name
	 * 签名
	 * @return
	 */
	public static boolean isSign(ParamObject param) {
		boolean flag = false;
		// 验证签名
		if(param!=null){
			Date date=new Date();
			String str1="";
			String str2="";
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
			String ndate=sdf.format(date);
			str1=MD5Util.MD5(MD5Util.MD5Sign(ndate, MD5Util.MD5KEY));
			str1=MD5Util.MD5Sign(param.getUid() + param.getName()+str1+"sign", MD5Util.MD5KEY);
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(date);
			rightNow.add(Calendar.MINUTE,1);
			date=rightNow.getTime();
			ndate=sdf.format(date);
			str2=MD5Util.MD5(MD5Util.MD5Sign(ndate, MD5Util.MD5KEY));
			str2=MD5Util.MD5Sign(param.getUid() + param.getName()+str2+"sign", MD5Util.MD5KEY);
			String fa=param.getSign().substring(11,12);
			str1=str1.substring(0,11)+fa+str1.substring(11,str1.length()-1);
			str2=str2.substring(0,11)+fa+str2.substring(11,str2.length()-1);
			if (str1.equals(param.getSign())||str2.equals(param.getSign())){
				flag = true;
			}
		}
		return flag;
	}
	
}